<?php

/**
 * Invoice form.
 *
 * @package    form
 * @subpackage invoice
 * @version    SVN: $Id: sfPropelFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $
 */
class InvoiceForm extends BaseInvoiceForm
{

  public function configure()
  {   
    if($this->isNew()){
      $this->validatorSchema['customer_iden'] = new sfValidatorString(array('required' => true));
      $this->validatorSchema['invoice_iden'] = new sfValidatorString(array('required' => true));
    }
    $this->widgetSchema['customer_iden'] = new sfWidgetFormInputHidden();
    $this->widgetSchema['delivery_mode'] = 
      new sfWidgetFormSelect(
        array('choices'=>array_combine(sfConfig::get('app_selection_delivery_modes'),sfConfig::get('app_selection_delivery_modes')),)
      );
    $this->widgetSchema['invoice_type'] = 
      new sfWidgetFormSelect(
        array('choices'=>ListDocumentTypesPeer::getList('INVOICE'),)
      );
      
    $this->widgetSchema['vat_excempt_type'] = 
      new sfWidgetFormSelect(
        array(
          'choices'=>
            array_combine(
              sfConfig::get('app_selection_vat_excempt_type'),
              sfConfig::get('app_selection_vat_excempt_type')
            ),
        )
      );
      
    $this->widgetSchema['days_due']->setAttribute('readonly',true);
    
    $this->widgetSchema['invoice_name']->setAttribute('size',30);
    
    $this->widgetSchema->setLabels(
      array(
        'invoice_id'=>'Invoice#',
        'invoice_type'=>'Type',
        'invoice_name'=>'Invoice To',
        'owner_iden'=>'Supplier',
        'delivery_mode'=>'Delivery',
        'delivery_date'=>'Delivery Date',
        'delivery_details'=>'Delivery Instructions',
        'terms_name'=>'Terms Name',
        'days_due'=>'Days Due',
        'date_received'=>'Date Received',
        'received_by'=>'Received By',
        'remarks'=>'Remarks',
        'delivery_date'=>'Delivery Date',
        'vat_excempt_type'=>'VAT Excempt',
      )
    );
    
    $this->validatorSchema->setPostValidator(
      new sfValidatorCallback(
        array(
          'callback'=>array(
            $this,'validateDeliveryDate',
          ),
        )
      )
    );

  }
  
  public function setIssueDate($issue_date){
    $this->issue_date = $issue_date;
  }
  
  public function validateDeliveryDate($validator,$values){
    $delivery_date = $values['delivery_date'];
    $error = array();
    if($this->issue_date > $delivery_date){
      $error['delivery_date'] = new sfValidatorError($validator,'Deivery date cannot be less than issue date');
    }
    
    if(count($error)>0)
      throw new sfValidatorErrorSchema($validator,$error);
      
    return $values;
  }
  
  public function setCustomerDefaults($contact){
  }
  
  public function getUrl(){
    if($this->isNew())
      return 'invoice/insert';
    else
      return 'invoice/update';
      
  }
  
  public function setIdReadOnly(){
    $this->widgetSchema['invoice_id']->setAttribute('readonly',true);
  }
  
  public function setIdTypeReadOnly(){
    $this->widgetSchema['invoice_type']->setAttribute('onFocus','this.blur();');
    $this->widgetSchema['invoice_id']->setAttribute('readonly',true);
  }
  
  
}
